Page({
    data: {
        x: 0,
        y: 300,
        optionsListData: [],
        movableViewPosition: {
            x: 0,
            y: 0,
            className1: "none",
            data: {}
        },
        scrollPosition: {
            everyOptionCell: 120,
            top: 0,
            scrollTop: 0,
            scrollY: true,
            scrollViewHeight: 0,
            scrollViewWidth: 375,
            windowViewHeight: 120,
        },
        selectItemInfo: {
            sName: "",
            sDtSecCode: "",
            sCode: "",
            selectIndex: -1,
            selectPosition: 0,
        },
        navList: [],
        navmovableViewPosition: {
            x: 0,
            y: 0,
            className1: "none",
            data: {}
        },
        navscrollPosition: {
            everyOptionCell: 40,
            top: 0,
            scrollTop: 0,
            scrollY: true,
            scrollViewHeight: 0,
            scrollViewWidth: 375,
            windowViewHeight: 40,
        },
        navselectItemInfo: {
            sDtSecCode: "",
            selectIndex: -1,
            selectPosition: 0,
        },
        nav_id: null,
    },

    transmit() {
        const that = this;
        console.log("转发事件")
        wx.navigateTo({
            url: '/pages/addGoods/addGoods',
        })
    },
    //价格/库存
    eduitprince(e) {
        this.popup.changeRange(e);
    },
    showTab: function (e) {
        console.log('this is showtabBar');
        console.log(e.detail);
    },
    //下架
    eduitxiajia() {
        console.log('this is 下架');
    },
    //编辑
    eduitinfo(e) {
        const that = this;
        var newId = e.target.dataset.id;
        var sduit_data = {};
        wx.navigateTo({
          url: `/pages/eduitGoods/eduitGoods?id=${newId}`
        })
    },





    //nav滚动高度
    navbindscroll: function (event) {
        var scrollTop = event.detail.scrollTop;
        this.setData({
            'navscrollPosition.scrollTop': scrollTop
        })
    },
    //navinfo
    navgetOptionInfo: function (code) {
        for (var i = 0, j = this.data.navList.length; i < j; i++) {
            var optionData = this.data.navList[i];
            if (optionData.sDtSecCode == code) {
                optionData.selectIndex = i;
                return optionData;
            }
        }
        return {};
    },
    //计算锚点
    navgetPositionDomByXY: function (potions) {
        var y = potions.y - this.data.navscrollPosition.top + this.data.navscrollPosition.scrollTop;
        var navList = this.data.navList;
        var everyOptionCell = this.data.navscrollPosition.everyOptionCell;
        for (var i = 0, j = navList.length; i < j; i++) {
            if (y >= i * everyOptionCell && y < (i + 1) * everyOptionCell) {
                return navList[i];
            }
        }
        return navList[0];
    },
    //拖拽触发
    navdraggleTouch: function (event) {
        var touchType = event.type;
        switch (touchType) {
            case "touchstart":
                this.navscrollTouchStart(event);
                break;
            case "touchmove":
                this.navscrollTouchMove(event);
                break;
            case "touchend":
                this.navscrollTouchEnd(event);
                break;
        }
    },
    navscrollTouchStart: function (event) {
        // console.log(event);
        var firstTouchPosition = {
            x: event.changedTouches[0].pageX,
            y: event.changedTouches[0].pageY,
        }
        var domData = this.navgetPositionDomByXY(firstTouchPosition);
        //movable-area滑块位置处理
        var movableX = 0;
        var movableY = firstTouchPosition.y - this.data.navscrollPosition.top - this.data.navscrollPosition.everyOptionCell / 2;
        this.setData({
            navmovableViewPosition: {
                x: movableX,
                y: movableY,
                className1: "",
                data: domData
            }
        })
        var secCode = domData.sDtSecCode;
        var secInfo = this.navgetOptionInfo(secCode);

        secInfo.selectPosition = event.changedTouches[0].clientY;
        secInfo.selectClass = "dragSelected";
        this.data.navList[secInfo.selectIndex].selectClass = "dragSelected";
        var navList = this.data.navList;
        this.setData({
            'navscrollPosition.scrollY': false,
            navselectItemInfo: secInfo,
            navList: navList,
            'navscrollPosition.selectIndex': secInfo.selectIndex
        })
    },
    navscrollTouchMove: function (event) {
        var selectItemInfo = this.data.navselectItemInfo;
        var selectPosition = selectItemInfo.selectPosition;
        var moveDistance = event.changedTouches[0].clientY;
        var everyOptionCell = this.data.navscrollPosition.everyOptionCell;
        var navList = this.data.navList;
        var selectIndex = selectItemInfo.selectIndex;
        //movable-area滑块位置处理
        var movableX = 0;
        var movableY = event.changedTouches[0].pageY - this.data.navscrollPosition.top - this.data.navscrollPosition.everyOptionCell / 2;
        this.setData({
            navmovableViewPosition: {
                x: movableX,
                y: movableY,
                className1: "",
                data: this.data.navmovableViewPosition.data
            }
        })
        if (moveDistance - selectPosition > 0 && selectIndex < navList.length - 1) {
            if (navList[selectIndex].sDtSecCode == selectItemInfo.sDtSecCode) {
                navList.splice(selectIndex, 1);
                navList.splice(++selectIndex, 0, selectItemInfo);
                selectPosition += everyOptionCell;
            }
        }
        if (moveDistance - selectPosition < 0 && selectIndex > 0) {
            if (navList[selectIndex].sDtSecCode == selectItemInfo.sDtSecCode) {
                navList.splice(selectIndex, 1);
                navList.splice(--selectIndex, 0, selectItemInfo);
                selectPosition -= everyOptionCell;
            }
        }
        this.setData({
            'navselectItemInfo.selectPosition': selectPosition,
            'navselectItemInfo.selectIndex': selectIndex,
            navList: navList,
        });
    },
    navscrollTouchEnd: function (event) {
        // console.log(event);
        var navList = this.navoptionsDataTranlate(this.data.navList, "");
        this.setData({
            navList: navList,
            'navscrollPosition.scrollY': true,
            'navmovableViewPosition.className1': "none"
        })
    },
    //计算数据
    navoptionsDataTranlate: function (optionsList, selectClass) {
        for (var i = 0, j = optionsList.length; i < j; i++) {
            optionsList[i].selectClass = selectClass;
        }
        return optionsList;
    },
    //点击列表cell
    OptionInfo(value) {
        console.log(value.currentTarget.dataset.seccode, 5555);
        this.setData({
            nav_id: value.currentTarget.dataset.seccode,
        });
    },







    /*
    右侧商品信息
    */

    //滚动高度
    bindscroll: function (event) {
        var scrollTop = event.detail.scrollTop;
        this.setData({
            'scrollPosition.scrollTop': scrollTop
        })
    },
    //info
    getOptionInfo: function (code) {
        for (var i = 0, j = this.data.optionsListData.length; i < j; i++) {
            var optionData = this.data.optionsListData[i];
            if (optionData.sDtSecCode == code) {
                optionData.selectIndex = i;
                return optionData;
            }
        }
        return {};
    },
    //计算锚点
    getPositionDomByXY: function (potions) {
        var y = potions.y - this.data.scrollPosition.top + this.data.scrollPosition.scrollTop;
        var optionsListData = this.data.optionsListData;
        var everyOptionCell = this.data.scrollPosition.everyOptionCell;
        for (var i = 0, j = optionsListData.length; i < j; i++) {
            if (y >= i * everyOptionCell && y < (i + 1) * everyOptionCell) {
                return optionsListData[i];
            }
        }
        return optionsListData[0];
    },
    //拖拽触发
    draggleTouch: function (event) {
        var touchType = event.type;
        switch (touchType) {
            case "touchstart":
                this.scrollTouchStart(event);
                break;
            case "touchmove":
                this.scrollTouchMove(event);
                break;
            case "touchend":
                this.scrollTouchEnd(event);
                break;
        }
    },
    scrollTouchStart: function (event) {
        // console.log(event);
        var firstTouchPosition = {
            x: event.changedTouches[0].pageX,
            y: event.changedTouches[0].pageY,
        }
        var domData = this.getPositionDomByXY(firstTouchPosition);
        //movable-area滑块位置处理
        var movableX = 0;
        var movableY = firstTouchPosition.y - this.data.scrollPosition.top - this.data.scrollPosition.everyOptionCell / 2;
        this.setData({
            movableViewPosition: {
                x: movableX,
                y: movableY,
                className1: "",
                data: domData
            }
        })
        var secCode = domData.sDtSecCode;
        var secInfo = this.getOptionInfo(secCode);
        secInfo.selectPosition = event.changedTouches[0].clientY;
        secInfo.selectClass = "dragSelected";
        this.data.optionsListData[secInfo.selectIndex].selectClass = "dragSelected";
        var optionsListData = this.data.optionsListData;
        this.setData({
            'scrollPosition.scrollY': false,
            selectItemInfo: secInfo,
            optionsListData: optionsListData,
            'scrollPosition.selectIndex': secInfo.selectIndex
        })
    },
    scrollTouchMove: function (event) {
        var selectItemInfo = this.data.selectItemInfo;
        var selectPosition = selectItemInfo.selectPosition;
        var moveDistance = event.changedTouches[0].clientY;
        var everyOptionCell = this.data.scrollPosition.everyOptionCell;
        var optionsListData = this.data.optionsListData;
        var selectIndex = selectItemInfo.selectIndex;
        //movable-area滑块位置处理
        var movableX = 0;
        var movableY = event.changedTouches[0].pageY - this.data.scrollPosition.top - this.data.scrollPosition.everyOptionCell / 2;
        this.setData({
            movableViewPosition: {
                x: movableX,
                y: movableY,
                className1: "",
                data: this.data.movableViewPosition.data
            }
        })

        if (moveDistance - selectPosition > 0 && selectIndex < optionsListData.length - 1) {
            if (optionsListData[selectIndex].sDtSecCode == selectItemInfo.sDtSecCode) {
                optionsListData.splice(selectIndex, 1);
                optionsListData.splice(++selectIndex, 0, selectItemInfo);
                selectPosition += everyOptionCell;
            }
        }
        if (moveDistance - selectPosition < 0 && selectIndex > 0) {
            if (optionsListData[selectIndex].sDtSecCode == selectItemInfo.sDtSecCode) {
                optionsListData.splice(selectIndex, 1);
                optionsListData.splice(--selectIndex, 0, selectItemInfo);
                selectPosition -= everyOptionCell;
            }
        }
        this.setData({
            'selectItemInfo.selectPosition': selectPosition,
            'selectItemInfo.selectIndex': selectIndex,
            optionsListData: optionsListData,
        });
    },
    scrollTouchEnd: function (event) {
        var optionsListData = this.optionsDataTranlate(this.data.optionsListData, "");
        this.setData({
            optionsListData: optionsListData,
            'scrollPosition.scrollY': true,
            'movableViewPosition.className1': "none"
        })
    },
    //计算数据
    optionsDataTranlate: function (optionsList, selectClass) {
        for (var i = 0, j = optionsList.length; i < j; i++) {
            optionsList[i].selectClass = selectClass;
        }
        return optionsList;
    },
    onLoad: function (options) {
        var systemInfo = wx.getSystemInfoSync();
        var optionsList = [{
            "sCode": "600879",
            "sDtSecCode": "0101600879",
            "sName": "航天电子航天电子航天电子",
            "original_img": 'https://groupbuy.lnxinlongjia.com/public/upload/store/0/brand/7b6a597593a8ce5814e38adbbcadef9c.jpg',
            "store_count": "122",
            "sales_sum": "450",
            "shop_price": "34.5",
        }, {
            "sCode": "00700",
            "sDtSecCode": "020100700",
            "sName": "腾讯控股",
            "original_img": 'https://groupbuy.lnxinlongjia.com/public/upload/store/0/brand/7b6a597593a8ce5814e38adbbcadef9c.jpg',
            "store_count": "122",
            "sales_sum": "450",
            "shop_price": "34.5",

        }, {
            "sCode": "000759",
            "sDtSecCode": "0001000759",
            "sName": "中百集团",
            "original_img": 'https://groupbuy.lnxinlongjia.com/public/upload/store/0/brand/7b6a597593a8ce5814e38adbbcadef9c.jpg',
            "store_count": "122",
            "sales_sum": "450",
            "shop_price": "34.5",
        }, {
            "sCode": "000651",
            "sDtSecCode": "0001000651",
            "sName": "格力电器",
            "original_img": 'https://groupbuy.lnxinlongjia.com/public/upload/store/0/brand/7b6a597593a8ce5814e38adbbcadef9c.jpg',
            "store_count": "122",
            "sales_sum": "450",
            "shop_price": "34.5",
        }, {
            "sCode": "600036",
            "sDtSecCode": "0101600036",
            "sName": "招商银行",
            "original_img": 'https://groupbuy.lnxinlongjia.com/public/upload/store/0/brand/7b6a597593a8ce5814e38adbbcadef9c.jpg',
            "store_count": "122",
            "sales_sum": "450",
            "shop_price": "34.5",
        }, {
            "sCode": "600518",
            "sDtSecCode": "0101600518",
            "sName": "康美药业",
            "original_img": 'https://groupbuy.lnxinlongjia.com/public/upload/store/0/brand/7b6a597593a8ce5814e38adbbcadef9c.jpg',
            "store_count": "122",
            "sales_sum": "450",
            "shop_price": "34.5",
        }, {
            "sCode": "601238",
            "sDtSecCode": "0101601238",
            "sName": "广汽集团",
            "original_img": 'https://groupbuy.lnxinlongjia.com/public/upload/store/0/brand/7b6a597593a8ce5814e38adbbcadef9c.jpg',
            "store_count": "122",
            "sales_sum": "450",
            "shop_price": "34.5",
        }, {
            "sCode": "601989",
            "sDtSecCode": "0101601989",
            "sName": "中国重工",
            "original_img": 'https://groupbuy.lnxinlongjia.com/public/upload/store/0/brand/7b6a597593a8ce5814e38adbbcadef9c.jpg',
            "store_count": "122",
            "sales_sum": "450",
            "shop_price": "34.5",
        }, {
            "sCode": "600519",
            "sDtSecCode": "0101600519",
            "sName": "贵州茅台",
            "original_img": 'https://groupbuy.lnxinlongjia.com/public/upload/store/0/brand/7b6a597593a8ce5814e38adbbcadef9c.jpg',
            "store_count": "122",
            "sales_sum": "450",
            "shop_price": "34.5",
        }, {
            "sCode": "002230",
            "sDtSecCode": "0001002230",
            "sName": "科大讯飞",
            "original_img": 'https://groupbuy.lnxinlongjia.com/public/upload/store/0/brand/7b6a597593a8ce5814e38adbbcadef9c.jpg',
            "store_count": "122",
            "sales_sum": "450",
            "shop_price": "34.5",
        }, {
            "sCode": "002431",
            "sDtSecCode": "0001002431",
            "sName": "棕榈股份",
            "original_img": 'https://groupbuy.lnxinlongjia.com/public/upload/store/0/brand/7b6a597593a8ce5814e38adbbcadef9c.jpg',
            "store_count": "122",
            "sales_sum": "450",
            "shop_price": "34.5",
        }, {
            "sCode": "01357",
            "sDtSecCode": "020101357",
            "sName": "美图公司",
            "original_img": 'https://groupbuy.lnxinlongjia.com/public/upload/store/0/brand/7b6a597593a8ce5814e38adbbcadef9c.jpg',
            "store_count": "122",
            "sales_sum": "450",
            "shop_price": "34.5",
        }, {
            "sCode": "300222",
            "sDtSecCode": "0001300222",
            "sName": "科大智能",
            "original_img": 'https://groupbuy.lnxinlongjia.com/public/upload/store/0/brand/7b6a597593a8ce5814e38adbbcadef9c.jpg',
            "store_count": "122",
            "sales_sum": "450",
            "shop_price": "34.5",
        }, {
            "sCode": "300131",
            "sDtSecCode": "0001300131",
            "sName": "英唐智控",
            "original_img": 'https://groupbuy.lnxinlongjia.com/public/upload/store/0/brand/7b6a597593a8ce5814e38adbbcadef9c.jpg',
            "store_count": "122",
            "sales_sum": "450",
            "shop_price": "34.5",
        }, {
            "sCode": "001696",
            "sDtSecCode": "0001001696",
            "sName": "宗申动力",
            "original_img": 'https://groupbuy.lnxinlongjia.com/public/upload/store/0/brand/7b6a597593a8ce5814e38adbbcadef9c.jpg',
            "store_count": "122",
            "sales_sum": "450",
            "shop_price": "34.5",
        }, {
            "sCode": "600503",
            "sDtSecCode": "0101600503",
            "sName": "华丽家族",
            "original_img": 'https://groupbuy.lnxinlongjia.com/public/upload/store/0/brand/7b6a597593a8ce5814e38adbbcadef9c.jpg',
            "store_count": "122",
            "sales_sum": "450",
            "shop_price": "34.5",
        }, {
            "sCode": "300154",
            "sDtSecCode": "0001300154",
            "sName": "瑞凌股份",
            "original_img": 'https://groupbuy.lnxinlongjia.com/public/upload/store/0/brand/7b6a597593a8ce5814e38adbbcadef9c.jpg',
            "store_count": "122",
            "sales_sum": "450",
            "shop_price": "34.5",
        }, {
            "sCode": "000001",
            "sDtSecCode": "0105000001",
            "sName": "上证指数",
            "original_img": 'https://groupbuy.lnxinlongjia.com/public/upload/store/0/brand/7b6a597593a8ce5814e38adbbcadef9c.jpg',
            "store_count": "122",
            "sales_sum": "450",
            "shop_price": "34.5",
        }];

        optionsList = this.optionsDataTranlate(optionsList, "");
        // 开始加载页面
        var scrollViewHeight = systemInfo.windowHeight;
        var scrollViewWidth = systemInfo.windowWidth;
        this.setData({
            nav_id: '0101600879',
            optionsListData: optionsList,
            'scrollPosition.scrollViewWidth': scrollViewWidth,
            'scrollPosition.scrollViewHeight': scrollViewHeight,
            'scrollPosition.windowViewHeight': systemInfo.windowHeight, //可使用窗口高度
        });


        //nav
        var navList = [{
                'sName': '采收记录1',
                'sDtSecCode': '1'
            }, {
                'sName': '采收记录2',
                'sDtSecCode': '2'
            }, {
                'sName': '采收记录3',
                'sDtSecCode': '3'
            }, {
                'sName': '采收记录4',
                'sDtSecCode': '4'
            }, {
                'sName': '采收记录5',
                'sDtSecCode': '5'
            }, {
                'sName': '采收记录6',
                'sDtSecCode': '6'
            }, {
                'sName': '采收记录7',
                'sDtSecCode': '7'
            }, {
                'sName': '采收记录9',
                'sDtSecCode': '9'
            }, {
                'sName': '采收记录10',
                'sDtSecCode': '10'
            }, {
                'sName': '采收记录11',
                'sDtSecCode': '11'
            }, {
                'sName': '采收记录12',
                'sDtSecCode': '12'
            }, {
                'sName': '采收记录13',
                'sDtSecCode': '13'
            }, {
                'sName': '采收记录14',
                'sDtSecCode': '14'
            }, {
                'sName': '采收记录15',
                'sDtSecCode': '15'
            }, {
                'sName': '采收记录16',
                'sDtSecCode': '16'
            }, {
                'sName': '采收记录17',
                'sDtSecCode': '17'
            }, {
                'sName': '采收记录18',
                'sDtSecCode': '18'
            }, {
                'sName': '采收记录19',
                'sDtSecCode': '19'
            }],
            navList = this.optionsDataTranlate(navList, "");
        // 开始加载页面
        var scrollViewHeight = systemInfo.windowHeight;
        var scrollViewWidth = systemInfo.windowWidth;
        this.setData({
            nav_id: '1',
            navList: navList,
            'navscrollPosition.scrollViewWidth': scrollViewWidth,
            'navscrollPosition.scrollViewHeight': scrollViewHeight,
            'navscrollPosition.windowViewHeight': systemInfo.windowHeight, //可使用窗口高度
        });

    },





    onShow: function () {
        // 页面显示
        console.log("onShow");

    },
    onReady: function () {
        // 页面渲染完成
        this.popup = this.selectComponent("#popup");
        console.log("onReady");
    },
    onHide: function () {
        console.log(22);
        // 页面隐藏
        console.log("onHide");
    },
    onUnload: function () {
        // 页面关闭
        console.log("onUnload");
    },
});